<template>
  <basic-container>
    <avue-crud
      :option="option"
      :data="data"
      :page="page"
      @row-del="rowDel"
      v-model="form"
      @row-update="rowUpdate"
      @row-save="rowSave"
      :before-open="beforeOpen"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <el-button type="primary" size="small" icon="el-icon-plus" plain @click="handleAdd">新增活动</el-button>
      </template>
      <template slot-scope="scope" slot="menu">
        <el-button type="text" @click="editCompany(scope.row)">活动福利</el-button>
      </template>
      <template slot-scope="{row}" slot="category">
        <el-tag>{{row.categoryName}}</el-tag>
      </template>
    </avue-crud>
  </basic-container>
</template>
<script>
import {getSearch, getList, remove, update, add, getDetail } from "@/api/activities/act";
import { mapGetters } from "vuex";

export default {
  data() {
    return {
      form: {},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        tip: false,
        border: true,
        index: false,
        viewBtn: false,
        selection: false,
        columnBtn: false,
        refreshBtn: false,
        searchBtn: false,
        editBtn: true,
        labelWidth: 130,
        addBtn: false,
        column: [
          {
            label: "活动名称",
            prop: "name",
            search: true,
            rules: [
              {
                required: true,
                message: "请输入通知标题",
                trigger: "blur"
              }
            ]
          },
          {
            label: "活动开始时间",
            prop: "startTime",
            type: "date",
            format: "yyyy-MM-dd hh:mm:ss",
            valueFormat: "yyyy-MM-dd hh:mm:ss",
            rules: [
              {
                required: true,
                message: "请输入通知日期",
                trigger: "blur"
              }
            ]
          },
          {
            label: "活动结束时间",
            prop: "endTime",
            type: "date",
            format: "yyyy-MM-dd hh:mm:ss",
            valueFormat: "yyyy-MM-dd hh:mm:ss",
            rules: [
              {
                required: true,
                message: "请输入通知日期",
                trigger: "blur"
              }
            ]
          },
          {
            label: "报名开始时间",
            prop: "signTime",
            type: "date",
            format: "yyyy-MM-dd hh:mm:ss",
            valueFormat: "yyyy-MM-dd hh:mm:ss",
            rules: [
              {
                required: true,
                message: "请输入通知日期",
                trigger: "blur"
              }
            ]
          },
          {
            label: "报名结束时间",
            prop: "deadLine",
            type: "date",
            format: "yyyy-MM-dd hh:mm:ss",
            valueFormat: "yyyy-MM-dd hh:mm:ss",
            rules: [
              {
                required: true,
                message: "请输入通知日期",
                trigger: "blur"
              }
            ]
          },
          {
            label: "活动名额",
            prop: "counts",
            rules: [
              {
                required: true,
                message: "请输入活动名额",
                trigger: "blur"
              }
            ]
          },
          {
            label: "发布时间",
            prop: "deadLine",
            type: "date",
            format: "yyyy-MM-dd hh:mm:ss",
            valueFormat: "yyyy-MM-dd hh:mm:ss",
            rules: [
              {
                required: true,
                message: "请输入通知日期",
                trigger: "blur"
              }
            ]
          },
          {
            label: "状态",
            prop: "state"
          },
          // {
          //   label: "实付金额",
          //   prop: "photoId"
          // },
          //   {
          //     label: "报名时间",
          //     prop: "releaseTimeRange",
          //     type: "datetimerange",
          //     format: "yyyy-MM-dd hh:mm:ss",
          //     valueFormat: "yyyy-MM-dd hh:mm:ss",
          //     hide: true,
          //     addDisplay: false,
          //     editDisplay: false,
          //     viewDisplay: false,
          //     search: false,
          //     rules: [
          //       {
          //         required: true,
          //         message: "请输入通知时间",
          //         trigger: "blur"
          //       }
          //     ]
          //   },

          {
            label: "通知内容",
            prop: "content",
            span: 24,
            minRows: 6,
            type: "textarea"
          }
        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.notice_add, false),
        viewBtn: this.vaildData(this.permission.notice_view, false),
        delBtn: this.vaildData(this.permission.notice_delete, false),
        editBtn: this.vaildData(this.permission.notice_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  methods: {
    editCompany(item) {
      console.log(item)
      let value = {
        id: item.id,
        name: item.name
      }
       this.$router.push({
        path: "/monitor/myAct?id=" + JSON.stringify(value)
      });
    },
    handleAdd() {
      this.$router.push({ path: "/monitoer/add" });
    },
    rowSave(row, loading, done) {
      add(row).then(
        () => {
          loading();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        },
        error => {
          done();
          console.log(error);
        }
      );
    },
    rowUpdate(row, index, loading, done) {
      update(row).then(
        () => {
          loading();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        },
        error => {
          done();
          console.log(error);
        }
      );
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    searchReset() {
      this.onLoad(this.page);
    },
    searchChange(params) {
       console.log(params);
       let value = 'getSearch';
      this.onLoad(this.page, params, value);
    },
    selectionChange(list) {
      debugger;
      this.selectionList = list;
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      console.log(this.form.id);
      // this.$router.push({ path: "/monitoer/add" });
       this.$router.push({
        path: "/monitoer/add?id=" + this.form.id
      });
      // if (["edit", "view"].includes(type)) {
      //   getDetail(this.form.id).then(res => {
      //     this.form = res.data.data;
      //   });
      // }
      // done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}, item) {
     
      const { releaseTimeRange } = params;
      let values = {
        ...params
      };
      // if (releaseTimeRange) {
      //   values = {
      //     ...params,
      //     releaseTime_gt: releaseTimeRange[0],
      //     releaseTime_lt: releaseTimeRange[1]
      //   };
      //   values.releaseTimeRange = null;
      // }
      console.log(values);
      if (item === 'getSearch') {
      getSearch(page.currentPage, page.pageSize,values).then(res => {
              const data = res.data.data;
              this.page.total = data.total;
              this.data = data.records;
            });
      } else {
        getList(page.currentPage, page.pageSize, values).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
      });
      }
    }
  }
};
</script>
<style>
</style>
